package org.kmansoft.shared.FasterScrollerTest;

import java.util.HashSet;
import java.util.Set;

import org.kman.ListLayoutTest.R;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.TextView;
import android.widget.Toast;

public class ListLayoutTestActivity extends Activity {

	private static final String TAG = "ListLayoutTestActivity";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		mFasterScroller = (FasterScrollerView) findViewById(R.id.fastscroll);
		mFasterScroller.setFastScrollEnabled(true);
		
		mListView = (ListView) findViewById(R.id.list);
		mListView.setAdapter(new TestAdapter(LayoutInflater.from(this)));

		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Toast.makeText(view.getContext(), "Item click: " + position, Toast.LENGTH_SHORT).show();
			}
		});

		mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
				Toast.makeText(view.getContext(), "Item LONG click: " + position, Toast.LENGTH_SHORT).show();
				return true;
			}
		});
	}
	
	private FasterScrollerView mFasterScroller;
	private ListView mListView;

	private static final int BASE_COUNT = 40;
	private static final String[] BASE_SECTIONS = new String[] { "Section One 1", "Section Two 2", "Section Three 3", "Section Four 4" }; 
			
	private static final int EXTENDED_COUNT = 60;
	private static final String[] EXTENDED_SECTIONS = new String[] { "Section One 1", "Section Two 2", "Section Three 3", "Section Four 4", "Section Five 5", "Section Six 6" };

	class TestAdapter extends BaseAdapter implements SectionIndexer {
		
		TestAdapter(LayoutInflater inflater) {
			mInflater = inflater;
			mSelected = new HashSet<Long>();
		}

		@Override
		public int getCount() {
			return mExtended ? EXTENDED_COUNT : BASE_COUNT;
		}

		@Override
		public Object getItem(int position) {
			return this;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			if (view == null) {
				view = mInflater.inflate(R.layout.list_item_layout, null);
			}
			
			TextView tv = (TextView) view.findViewById(R.id.message_item_subject);
			tv.setText("Subject: " + position);

			long _id = getItemId(position);
			CheckBox cb = (CheckBox) view.findViewById(R.id.message_is_selected);
			cb.setTag(_id);

			if (mSelected.contains(new Long(_id))) {
				cb.setChecked(true);
			} else {
				cb.setChecked(false);
			}

			cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					long _id = (Long) buttonView.getTag();
					if (isChecked) {
						mSelected.add(_id);
					} else {
						mSelected.remove(_id);
					}
				}
			});
			// cb.setClickable(false);
			cb.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Log.i(TAG, "onClick for checkbox");
				}
			});

			view.setOnTouchListener(new View.OnTouchListener() {

				boolean tracking = false;

				@Override
				public boolean onTouch(View v, MotionEvent event) {
					Log.i(TAG, "onTouch for item layout: " + event);

					boolean res = false;
					if (event.getAction() == MotionEvent.ACTION_DOWN) {
						if (event.getX() < 100) {
							tracking = true;
							res = true;
						}
					} else if (event.getAction() == MotionEvent.ACTION_UP && tracking) {
						if (event.getX() < 100) {
							CheckBox cb = (CheckBox) v.findViewById(R.id.message_is_selected);
							// cb.toggle();
							cb.performClick();
							tracking = false;
							res = true;
						}
					}
					return res;
				}
			});
			
			if (!mExtended && position == BASE_COUNT - 1){
				mListView.post(new Runnable() {
					@Override
					public void run() {
					mExtended = true;
					notifyDataSetChanged();
					mFasterScroller.updateSections(true);
					}
				});
			}

			return view;
		}
		
		@Override
		public int getPositionForSection(int section) {
			return section * 10;
		}

		@Override
		public int getSectionForPosition(int position) {
			return position / 10;
		}

		@Override
		public Object[] getSections() {
			return mExtended ? EXTENDED_SECTIONS : BASE_SECTIONS;
		}

		LayoutInflater mInflater;
		Set<Long> mSelected;
		boolean mExtended;
	}
}